/**
 * 功能描述
 */

define(function(require,exports,module){
	var Subnav = require("subnav");
	var subnavEvents = require("../handler/subnavEvents");
	var aw = require("ajaxwrapper");
	var roomPanoramaHandler = require("../handler/roomSelectHandler");
	var user = require("store").get("user");
	
	module.exports = {
			initComponent : function (params, widget) {
			var subnav = new Subnav({
				parentNode: ".J-subnav",
				model: {
					title: "换房调整",
					items: [{
        				id:"search",
        				type:"search",
        				placeholder:"联系人",
        				handler : function(str){
        					var grid = widget.get("grid");
        					var subnav = widget.get("subnav");
        					aw.ajax({
        						url:"api/changeroom/searchname",
        						data:{
        							s:str,
                        			"servicePoint" : subnav.getValue("servicePoint"),
                        			"contactName" : str,
                        			"destination" : subnav.getValue("destination"),
                        			"startDate" : subnav.getValue("time").start,
                        			"endDate" : subnav.getValue("time").end,
//                        			"orderString" : "grantDate desc",
        							fetchProperties:"*"
        						},
        						dataType:"json",
        						success:function(data){
        							grid.setData(data);
        							widget.get("subnav").setValue("search", "");
        						}
        					});
        				}
					},{
						id: "servicePoint",
						type: "buttongroup",
//						url: "api/servicepoint/operableservicepoints",
//						lazy: true,
						items:user.privateServicePoints,
						tip: "服务点",
						keyField: "pkServicePoint",
						valueField: "name",
						handler: function(){
							widget.get("grid").refresh();
						}
					},{
						id: "destination",
						type: "buttongroup",
						tip: "目的地",
						url: "api/destination/query",
						keyField: "pkDestination",
						valueField: "name",
						all: {
							show: true
						},
						handler: function(){
							widget.get("grid").refresh();
						}
					},{
						id : "time",
						type : "daterange",
						tip: "入住时间",
						ranges : {
					        "本年": [moment().startOf("year"), moment().endOf("days")],
			                "今天": [moment().startOf("days"), moment().endOf("days")],
			                "本月": [moment().startOf("month"), moment().endOf("month")]
						},
						defaultRange: [moment().startOf("year"), moment().endOf("days")],
						handler : function(time){
							 widget.get("grid").refresh();
						}
					},{
						id: "save",
						type: "button",
						text: "保存",
						show: false,
						handler: function(){
							subnavEvents.saveAction(widget);
						}
					},{
						id: "return",
						type: "button",
						text: "返回",
						show: false,
						handler: function(){
        					widget.hide([".J-form",".J-editgrid",".J-detailgrid"]).show([".J-grid"]);
							widget.get("subnav").show(["search", "servicePoint", "destination", "time"]).hide(["save", "return"]);
							widget.get("subnav").setValue("search", "");
							widget.get("grid").refresh();
						}
					},{
						id: "consumsave",
						type: "button",
						text: "保存消费",
						show: false,
						handler: function(){
							subnavEvents.saveConsum(widget);
						}
					},{
						id: "consumreturn",
						type: "button",
						text: "返回",
						show: false,
						handler: function(){
							widget.show([".J-editgrid",".J-form"]).hide([".J-middleform",".J-consumpeditgrid",".J-consumpdetailgrid"]);
							if(widget.get("detail")){
								widget.get("subnav").show(["return"]);
							}else{
								widget.get("subnav").show(["return","save"]);
							}
							widget.get("subnav").hide(["consumsave","consumreturn"]);
						}
					},{
						id : "building",
						text : "楼宇",
						tip : "楼宇",
						lazy:true,
						type : "buttongroup",
						keyField: "pkOrgBuilding",
        	            valueField: "name",
        	            url:"api/orgbuilding/query",
						handler:function(key,element){	
							subnav.load("roomType", {
		        				params:{
		        					orgBuilding: subnav.getValue("building"),
		        	            	fetchProperties:"pkRoomType,name"
		        	            },
		                        callback: function(data) {
		                            subnav.setData("roomType", data);
		                            roomPanoramaHandler.setData(widget.get("curStatus"),widget);
		                        }
		                    });
						}
					},{
						id : "roomType",
						text : "房型",
						tip : "房型",
						lazy:true,
						type : "buttongroup",
						keyField: "pkRoomType",
        	            valueField: "name",
        	            url:"api/groupreserve/queryroomtype",
						handler:function(key,element){	
							roomPanoramaHandler.setData(widget.get("curStatus"),widget);
						}
					},{
						id: "roomStateReturn",
						type: "button",
						text: "返回",
						show: false,
						handler: function(){
							widget.hide([".J-roomStatus"]).show([".J-form",".J-editgrid "]);
							widget.get("subnav").hide(["building","roomType","roomStateReturn"]).show(["save","return"]);
						}
					}]
				}
			});
			widget.set("subnav",subnav);
		}
	};
});